package com.myh.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.myh.gulimall.product.service.AttrAttrgroupRelationService;
import com.myh.gulimall.product.service.AttrService;
import com.myh.gulimall.product.service.CategoryService;
import com.myh.gulimall.product.vo.AttrGroupRelationVo;
import com.myh.gulimall.product.vo.AttrGroupWithAttrVo;
import com.myh.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.myh.gulimall.product.entity.AttrGroupEntity;
import com.myh.gulimall.product.service.AttrGroupService;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.R;



/**
 * 属性分组
 *
 * @author myh
 * @email 2722407149@qq.com
 * @date 2024-01-01 14:39:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    /**
     * 获取分类属性分组
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /*
    * 获取属性分组与属性的关联
    * */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getAttrgroupRelAttrs(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrVo> attrVos = attrGroupService.getAttrgroupRelAttrs(attrgroupId);
        return R.ok().put("data", attrVos);
    }

    /*
    * 移除属性分组与属性的关联
    * */
    @PostMapping ("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.deleteByIds(vos);
        return R.ok();
    }

    /*
    * 获取属性分组没有关联的其他属性
    * */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(attrgroupId, params);
        return R.ok().put("page", page);
    }

    /*
    *添加属性与分组关联关系
    * */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos ){
        attrGroupService.addRelation(vos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAllAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> vos = attrGroupService.getAllAttrGroupWithAttr(catelogId);
        return R.ok().put("data", vos);
    }

}
